package com.li.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.li.mapper.ClueMapper;
import com.li.mapper.ContractMapper;
import com.li.mapper.ImageReportMapper;
import com.li.pojo.dto.CountDto;
import com.li.pojo.po.Contract;
import com.li.pojo.po.PageResult;
import com.li.pojo.vo.ImageReportVo;
import com.li.pojo.vo.YCount;
import com.li.service.ContractService;
import com.li.service.ImageReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Service
public class ImageReportServiceImpl implements ImageReportService {
    @Autowired
    private ImageReportMapper imageReportMapper;
    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private ClueMapper clueMapper;

    @Override
    public ImageReportVo contractStatistics(LocalDate beginCreateTime, LocalDate endCreateTime) {
        List<LocalDate> allDays = getAllDays(beginCreateTime, endCreateTime);
        List<String> xAxis=new ArrayList<>();
        List<Integer> totalList=new ArrayList<>();
        List<Integer> newList=new ArrayList<>();
        for (LocalDate oneDay : allDays) {
            Integer totalContract=imageReportMapper.groupContractByTime(null,oneDay);
            Integer newContract=imageReportMapper.groupContractByTime(oneDay,oneDay);

            xAxis.add(oneDay.toString());
            totalList.add(totalContract);
            newList.add(newContract);
        }
        YCount axis1 = new YCount( "新增客户数",newList);
        YCount axis2 = new YCount("客户总数",totalList);
        List<YCount> yAxisList=new ArrayList<>();
        yAxisList.add(axis1);
        yAxisList.add(axis2);

        return new ImageReportVo(xAxis,yAxisList);
    }

    @Override
    public ImageReportVo cluesStatistics(LocalDate beginCreateTime, LocalDate endCreateTime) {
        List<LocalDate> allDays = getAllDays(beginCreateTime, endCreateTime);
        List<String> xAxis=new ArrayList<>();
        List<Integer> totalList=new ArrayList<>();
        List<Integer> newList=new ArrayList<>();
        for (LocalDate oneDay : allDays) {
            Integer totalContract=imageReportMapper.groupCluesByTime(null,oneDay);
            Integer newContract=imageReportMapper.groupCluesByTime(oneDay,oneDay);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            xAxis.add(oneDay.format(formatter).toString());
            totalList.add(totalContract);
            newList.add(newContract);
        }
        YCount axis1 = new YCount("新增线索数量",newList);
        YCount axis2 = new YCount( "线索总数量",totalList);
        List<YCount> yAxisList=new ArrayList<>();

        yAxisList.add(axis2);
        yAxisList.add(axis1);

        return new ImageReportVo(xAxis,yAxisList);
    }

    @Override
    public ImageReportVo salesStatistics(LocalDate beginCreateTime, LocalDate endCreateTime) {
        List<LocalDate> allDays = getAllDays(beginCreateTime, endCreateTime);
        List<String> xAxis=new ArrayList<>();
        List<Integer> totalList=new ArrayList<>();

        for (LocalDate oneDay : allDays) {
            Integer totalContract=imageReportMapper.sumMoneyByTime(oneDay,oneDay);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            xAxis.add(oneDay.format(formatter).toString());
            totalList.add(totalContract);

        }

        YCount axis2 = new YCount( "销售统计",totalList);
        List<YCount> yAxisList=new ArrayList<>();
        yAxisList.add(axis2);
        return new ImageReportVo(xAxis,yAxisList);
    }

    @Override
    public PageResult findCountByPage(CountDto countDto) {
        Page<Contract> page = PageHelper.startPage(countDto.getPageNum(), countDto.getPageSize());
        List<Contract> list = contractMapper.findAll();
        return new PageResult(200,"分页查询成功",null,page.getResult(),page.getTotal());
    }

    @Override
    public PageResult cluesStatisticsList(CountDto countDto) {
        Page<Contract> page = PageHelper.startPage(countDto.getPageNum(), countDto.getPageSize());
        List<Contract> list = clueMapper.findAll();
        return new PageResult(200,"分页查询成功",null,page.getResult(),page.getTotal());
    }

    private List<LocalDate> getAllDays(LocalDate begin, LocalDate end) {
        ArrayList<LocalDate> list = new ArrayList<>();
        list.add(begin);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            list.add(begin);
        }
        return list;
    }
}
